package com.anyonavinfo.cpaddemo.view.mainview.miniradio;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.anyonavinfo.cpaddemo.R;

import java.util.ArrayList;


/**
 * Created by Drive on 2017/1/19.
 */

public class MiniRadioView extends LinearLayout implements View.OnClickListener, View.OnLongClickListener, MiniRadioContract.View {
    private ImageView imageViewOpen;
    private ImageButton imageButtonSeekDown, imageButtonSeekUp, imageButtonPlay;
    private Button buttonBand, buttonStation1, buttonStation2, buttonStation3;
    private TextView textViewBand, textViewFrequency, textViewFrqUnit;
    private MiniRadioPresenter presenter;
    private int preBand = -1, prePosition = 0, preVoiceChannel = -1;//1 radio 2 phone 3media

    private Context mContext;
    private String TAG = "RadioViewLog";

    public MiniRadioView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    public MiniRadioView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MiniRadioView(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View view = layoutInflater.inflate(R.layout.view_miniview_miniradio_miniradioview, this);
        mContext = context;
        imageViewOpen = (ImageView) view.findViewById(R.id.titlelayout_image_open);
        imageButtonSeekDown = (ImageButton) view.findViewById(R.id.searchlayout_imagebutton_seekdown);
        imageButtonSeekUp = (ImageButton) view.findViewById(R.id.searchlayout_imagebutton_seekup);
        imageButtonPlay = (ImageButton) view.findViewById(R.id.searchlayout_imagebutton_play);
        buttonStation1 = (Button) view.findViewById(R.id.stationlayout_button_station1);
        buttonStation2 = (Button) view.findViewById(R.id.stationlayout_button_station2);
        buttonStation3 = (Button) view.findViewById(R.id.stationlayout_button_station3);
        buttonBand = (Button) view.findViewById(R.id.titlelayout_button_band);
        textViewBand = (TextView) view.findViewById(R.id.frqlayout_textview_band);
        textViewFrequency = (TextView) view.findViewById(R.id.frqlayout_textview_frq);
        textViewFrqUnit = (TextView) view.findViewById(R.id.frqlayout_textview_frqunit);
        imageViewOpen.setOnClickListener(this);
        imageButtonSeekDown.setOnClickListener(this);
        imageButtonSeekUp.setOnClickListener(this);
        imageButtonPlay.setOnClickListener(this);
        buttonStation1.setOnClickListener(this);
        buttonStation2.setOnClickListener(this);
        buttonStation3.setOnClickListener(this);
        buttonStation1.setOnLongClickListener(this);
        buttonStation2.setOnLongClickListener(this);
        buttonStation3.setOnLongClickListener(this);
        buttonBand.setOnClickListener(this);
        presenter = new MiniRadioPresenter(context);
        presenter.setRaidoView(this);
        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.openApp(mContext);
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.titlelayout_image_open:
                presenter.openApp(mContext);
                break;
            case R.id.searchlayout_imagebutton_seekdown:
                presenter.seekDown();
                break;
            case R.id.searchlayout_imagebutton_seekup:
                presenter.seekUp();
                break;
            case R.id.searchlayout_imagebutton_play:
                presenter.playRadio();
                break;
            case R.id.stationlayout_button_station1:
                if (!buttonStation1.getText().toString().equals("电台1")) {
                    presenter.seekStation(1);
                }
                break;
            case R.id.stationlayout_button_station2:
                if (!buttonStation2.getText().toString().equals("电台2")) {
                    presenter.seekStation(2);
                }
                break;
            case R.id.stationlayout_button_station3:
                if (!buttonStation3.getText().toString().equals("电台3")) {
                    presenter.seekStation(3);
                }
                break;
            case R.id.titlelayout_button_band:
                presenter.changeBand();
                break;
            default:
                break;


        }

    }


    @Override
    public boolean onLongClick(View v) {
        switch (v.getId()) {
            case R.id.stationlayout_button_station1:
                presenter.saveStation(1);
                break;
            case R.id.stationlayout_button_station2:
                presenter.saveStation(2);
                break;
            case R.id.stationlayout_button_station3:
                presenter.saveStation(3);
                break;
        }
        return false;
    }

    @Override
    public void updateVoiceChannel(final int channel) {
        ((Activity) mContext).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (channel == 1 && preVoiceChannel != channel) {
                    imageButtonPlay.setBackgroundResource(R.drawable.icon_miniradioview_pause);
                } else if (preVoiceChannel == 1 && channel != preVoiceChannel) {
                    imageButtonPlay.setBackgroundResource(R.drawable.icon_miniradioview_play);
                }
                preVoiceChannel = channel;
            }
        });

    }

    @Override
    public void updateFrq(final int newBand, final int frq, final int samePosition, final int[] stations) {
        ((Activity) mContext).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (newBand == 0 && preBand == 1) {
                    buttonBand.setText(mContext.getResources().getString(R.string.band_am));
                    textViewBand.setText(mContext.getResources().getString(R.string.band_am));
                    textViewFrqUnit.setText("KHZ");
                    updateStationList(newBand, stations);
                } else if (newBand == 1 && preBand == 0) {
                    buttonBand.setText(mContext.getResources().getString(R.string.band_fm));
                    textViewBand.setText(mContext.getResources().getString(R.string.band_fm));
                    textViewFrqUnit.setText("MHZ");
                    updateStationList(newBand, stations);
                }
                preBand = newBand;
                if (newBand == 0) {
                    textViewFrequency.setText(frq + "");
                } else if (newBand == 1) {
                    textViewFrequency.setText(frq / 10.0 + "");
                }
                setButtonStationState(samePosition);
            }
        });

    }

    //更新整个电台列表
    @Override
    public void updateStationList(final int band, final int[] stations) {
        if (band == 0) {
            if (stations[0] > 531) {
                buttonStation1.setText("" + stations[0]);
            } else {
                buttonStation1.setText("电台1");
            }
            if (stations[1] > 531) {
                buttonStation2.setText("" + stations[1]);
            } else {
                buttonStation2.setText("电台2");
            }
            if (stations[2] > 531) {
                buttonStation3.setText("" + stations[2]);
            } else {
                buttonStation3.setText("电台3");
            }
        } else if (band == 1) {
            if (stations[3] > 875) {
                buttonStation1.setText("" + stations[3] / 10.0);
            } else {
                buttonStation1.setText("电台1");
            }
            if (stations[4] > 875) {
                buttonStation2.setText("" + stations[4] / 10.0);
            } else {
                buttonStation2.setText("电台2");
            }
            if (stations[5] > 875) {
                buttonStation3.setText("" + stations[5] / 10.0);
            } else {
                buttonStation3.setText("电台3");
            }
        }

    }

    //更新单个电台
    @Override
    public void updateStation(int position, int frq) {
        switch (position) {
            case 1:
                buttonStation1.setText("" + frq);
                break;
            case 2:
                buttonStation2.setText("" + frq);
                break;
            case 3:
                buttonStation3.setText("" + frq);
                break;
            case 4:
                buttonStation1.setText("" + frq / 10.0);
                break;
            case 5:
                buttonStation2.setText("" + frq / 10.0);
                break;
            case 6:
                buttonStation3.setText("" + frq / 10.0);
                break;
            default:
                return;
        }
        if (position > 3) {
            setButtonStationState(position - 3);
        } else {
            setButtonStationState(position);
        }
    }

    private void setButtonStationState(int position) {
        if (prePosition == position)
            return;
        switch (prePosition) {
            case 1:
                buttonStation1.setBackgroundResource(R.drawable.bg_miniradioview_station);
                break;
            case 2:
                buttonStation2.setBackgroundResource(R.drawable.bg_miniradioview_station);
                break;
            case 3:
                buttonStation3.setBackgroundResource(R.drawable.bg_miniradioview_station);
                break;
            default:
                break;
        }
        switch (position) {
            case 1:
                buttonStation1.setBackgroundResource(R.drawable.bg_miniradioview_station_focu);
                break;
            case 2:
                buttonStation2.setBackgroundResource(R.drawable.bg_miniradioview_station_focu);
                break;
            case 3:
                buttonStation3.setBackgroundResource(R.drawable.bg_miniradioview_station_focu);
                break;
            default:
                break;
        }
        prePosition = position;
    }

}
